﻿// (c) Copyright Webber-Cross Software Ltd.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.IO;
using System.Xml.Serialization;
using System.Linq;
using Microsoft.Phone.Shell;
using System.Xml;
using System.IO.IsolatedStorage;

namespace WebberCross.Obelisk
{
    public class PersistenceStorage
    {
        private static Type[] _knownTypes = null;

        public static Type[] KnownTypes
        {
            get { return _knownTypes; }
            set { _knownTypes = value; }
        }
        
        /// <summary>
        /// Serialises and stores an object
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="overwrite"></param>
        /// <returns></returns>
        public static bool StoreObjectToIsolated<T>(string key, T o, bool overwrite)
        {
            bool stored = false;

            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (overwrite || !storage.FileExists(key))
                {
                    using (IsolatedStorageFileStream fileStream = storage.OpenFile(key, FileMode.Create))
                    {
                        try
                        {
                            XmlSerializer xs = new XmlSerializer(typeof(T), KnownTypes);
                            xs.Serialize(fileStream, o);
                        }
                        catch (Exception)
                        {

                        }
                    }
                }
            }

            return stored;
        }

        /// <summary>
        /// Deserialises and retrieves
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetObjectFromIsolated<T>(string key)
        {
            T o = default(T);

            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(key))
                {
                    using (IsolatedStorageFileStream fileStream = storage.OpenFile(key, FileMode.Open))
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(T), KnownTypes);
                        o = (T)xs.Deserialize(fileStream);
                    }
                }
            }

            return o;
        }

        /// <summary>
        /// Removes an object
        /// </summary>
        /// <param name="key"></param>
        public static void RemoveObjectFromIsolated(string key)
        {
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(key))
                {
                    storage.DeleteFile(key);
                }
            }
        }
    }
}
